//
//  SelectStartingAndEndPoint.m
//  bluetoothDemo
//
//  Created by peng on 15/9/18.
//  Copyright © 2015年 palmap. All rights reserved.
//

#import "SelectStartingAndEndPoint.h"
#define ratioX(x) ([UIScreen mainScreen].bounds.size.width/320.0 )* x
#define ratioY(y) ([UIScreen mainScreen].bounds.size.height/568.0 )* y
#define kScreenHeight   [[UIScreen mainScreen] bounds].size.height
#define kScreenWidth    [[UIScreen mainScreen] bounds].size.width
#define FitCGRectMake(x , y , z , w )  CGRectMake(kScreenWidth*((x)/320.0), kScreenWidth*((y)/320.0), kScreenWidth*((z)/320.0), kScreenWidth*((w)/320.0))


@interface SelectStartingAndEndPoint()


@property (strong, nonatomic)  UIButton *startButton;
@property (strong, nonatomic)  UIButton *endButton;
@property (nonatomic,strong)UILabel* startRoomNameLabel;
@property (nonatomic,strong)UILabel* endRoomNameLabel;
@end

@implementation SelectStartingAndEndPoint

-(id)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        
        UIImageView* bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, ratioY(52))];
        bgImageView.image =[UIImage imageNamed:@""];
        bgImageView.userInteractionEnabled = YES;
        [self addSubview:bgImageView];
        
        self.startButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.startButton.frame = CGRectMake(0, 0, kScreenWidth/2,ratioY(52) );
      
        [self.startButton setImage:[UIImage imageNamed:@"起点"] forState:UIControlStateNormal];
        [self.startButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [self.startButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
        [self.startButton setImage:[UIImage imageNamed:@"起点1"] forState:UIControlStateSelected];
        [self.startButton addTarget:self action:@selector(addStartData) forControlEvents:UIControlEventTouchUpInside];
        self.endButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.endButton.frame = CGRectMake(kScreenWidth/2, 0, kScreenWidth/2, ratioY(52));
        
        [self.endButton setBackgroundImage:[UIImage imageNamed:@"终点"] forState:UIControlStateNormal];
        [self.endButton setBackgroundImage:[UIImage imageNamed:@"终点1"] forState:UIControlStateSelected];
        [self.endButton addTarget:self action:@selector(addEndData) forControlEvents:UIControlEventTouchUpInside];
        [bgImageView addSubview:self.startButton];
        [bgImageView addSubview:self.endButton];
       
        self.startRoomNameLabel = [[UILabel alloc] initWithFrame:FitCGRectMake(10, 0, kScreenWidth/2 -10,30)];
        self.startRoomNameLabel.backgroundColor     = [UIColor clearColor];
        self.startRoomNameLabel.font                = [UIFont systemFontOfSize:15.0];
        self.startRoomNameLabel.textColor           = [UIColor darkTextColor];
        [self addSubview:self.startRoomNameLabel];
        
        
        self.endRoomNameLabel = [[UILabel alloc] initWithFrame:FitCGRectMake(kScreenWidth/2, 0, kScreenWidth/2 -10,30)];
        self.endRoomNameLabel.backgroundColor     = [UIColor clearColor];
        self.endRoomNameLabel.font                = [UIFont systemFontOfSize:15.0];
        self.endRoomNameLabel.textColor           = [UIColor darkTextColor];
        [self addSubview:self.endRoomNameLabel];
    }
    return self;
}


-(void)addStartData{
   [self.delegate removeSelectImageViewWith:YES AndStart:NO andEnd:NO];
    if (self.selectStartData ==nil || self.selectStartData.location.x != self.selectData.location.x) {
        self.selectStartData = [[touchPointData alloc]init];
        self.selectStartData.floorID = self.selectData.floorID;
        self.selectStartData.location = self.selectData.location;
        self.startButton.selected = YES;
        [self.delegate addAnnotation:self.selectStartData.location ImageName:@"luoyao起点.png" ChangtoFloor:self.selectStartData.floorID];
        
        
    }else {
       
        [self.delegate removeSelectImageViewWith:NO AndStart:YES andEnd:NO];
        self.startButton.selected = NO;
        self.selectData = nil;
        self.selectStartData = nil;
         [self.delegate addAnnotation:self.selectStartData.location ImageName:@"luoyao起点.png" ChangtoFloor:self.selectStartData.floorID];
    }
    
    
    if (self.selectEndData != nil && self.selectStartData != nil) {
        [self.delegate shopPopoverViewnavigationWithStartData:self.selectStartData EndData:self.selectEndData];

        self.startButton.selected = NO;
        self.endButton.selected = NO;
        
    }
}

-(void)addEndData{
    
    
    if (self.selectEndData ==nil ||self.selectEndData.location.x != self.selectData.location.x) {
        self.selectEndData = [[touchPointData alloc]init];
        self.selectEndData.floorID = self.selectData.floorID;
        self.selectEndData.location = self.selectData.location;
        self.endButton.selected = YES;
        [self.delegate addAnnotation:self.selectEndData.location ImageName:@"luoyao终点.png" ChangtoFloor:self.selectEndData.floorID];
        [self.delegate removeSelectImageViewWith:YES AndStart:NO andEnd:NO];
        
    }else {
        [self.delegate removeSelectImageViewWith:NO AndStart:NO andEnd:YES];
        self.endButton.selected = NO;
        self.selectEndData = nil;
        self.selectData = nil;
    }
    
    
    if (self.selectStartData == nil ) {
        
        touchPointData* touchData = [self.delegate UserCurrentLocation];
        if (touchData) {
            
            self.selectStartData = [[touchPointData alloc]init];
            self.selectStartData.location = touchData.location;
            self.selectStartData.floorID = touchData.floorID;
            
        }
        if (self.selectStartData != nil) {
             [self.delegate addAnnotation:self.selectStartData.location ImageName:@"luoyao起点.png" ChangtoFloor:self.selectStartData.floorID];
        }
    }

    if (self.selectStartData != nil  && self.selectEndData != nil) {
        [self.delegate shopPopoverViewnavigationWithStartData:self.selectStartData EndData:self.selectEndData];

        self.startButton.selected = NO;
        self.endButton.selected = NO;
          [self.delegate changetoStartpointFloor];
    }
   
   
}

@end
